import re

import requests

"""
知识点：
1，正则匹配
2，自动编解码
3，下载流文件并保存
"""

url = 'http://www.36dj.com/xc/'

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'

}

response = requests.get(url=url,headers=headers)
response.encoding = response.apparent_encoding

result_list = re.findall('><a href="/play/(\d+).html" title=',response.text)
for index in result_list:
    link = f'http://www.36dj.com/play/{index}.html'
    response_1 = requests.get(url=link,headers=headers)
    response_1.encoding = response_1.apparent_encoding
    mp3_info = re.findall('var music_name = "(.*?)"; var info = { "playurl": "(.*?)" };',response_1.text)[0]
    title = mp3_info[0]
    mp3_url = 'http://tn1.72djapp.cn:8399/'+ mp3_info[1]
    #获取二进制文件
    mp3_content = requests.get(url=mp3_url,headers=headers).content
    #保存
    with open('mp3\\' + title + '.mp3',mode='wb') as f:
        f.write(mp3_content)






